package com.example.controller;

import com.example.common.Result;
import com.example.entity.OrganizationMember;
import com.example.entity.SelectMemberDTO;
import com.example.service.OrganizationMemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.List;

/**
 * 组织成员表前端操作接口
 **/
@RestController
@RequestMapping("/organizationMember")
public class OrganizationMemberController {

    @Resource
    private OrganizationMemberService organizationMemberService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody OrganizationMember member, @RequestParam Long leaderId) {
        organizationMemberService.add(member, leaderId);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody OrganizationMember member) {
        organizationMemberService.updateById(member);
        return Result.success();
    }
    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        organizationMemberService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        organizationMemberService.deleteBatch(ids);
        return Result.success();
    }


    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        OrganizationMember organizationMember = organizationMemberService.selectById(id);
        return Result.success(organizationMember);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(OrganizationMember organizationMember) {
        List<OrganizationMember> list = organizationMemberService.selectAll(organizationMember);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(OrganizationMember organizationMember,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<OrganizationMember> page = organizationMemberService.selectPage(organizationMember, pageNum, pageSize);
        return Result.success(page);
    }

    @GetMapping("/selectMember")
    public Result selectMember(SelectMemberDTO selectMemberDTO,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<OrganizationMember> page = organizationMemberService.selectMember(selectMemberDTO, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 根据用户ID查询组织成员信息
     */
    @GetMapping("/getByUserId/{userId}")
    public Result getByUserId(@PathVariable Long userId) {
        OrganizationMember member = organizationMemberService.getByUserId(userId);
        return Result.success(member);
    }

}